<?php

namespace backend\modules\platform\controllers;

use Yii;
use common\models\Region;
use common\models\RegionSearch;
use backend\models\BackendController;
use yii\web\NotFoundHttpException;
use yii\web\BadRequestHttpException;
use yii\filters\VerbFilter;

/**
 * RegionController implements the CRUD actions for Region model.
 */
class RegionController extends BackendController {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Region models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new RegionSearch();
        
        $query=Yii::$app->request->queryParams;
        !isset($query['RegionSearch']) && $query['RegionSearch']['parent_id']=0;

        $dataProvider = $searchModel->search($query);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Region model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Region model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Region();

        $post = Yii::$app->request->post();
        if (isset($post['Region']['parent_id'])) {
            $p = 0;
            foreach ($post['Region']['parent_id'] as $id) {
                if ($id == 0) {
                    break;
                }
                $p = $id;
            }
            $post['Region']['parent_id'] = $p;
        }

        if ($model->load($post) && $model->save()) {
            $this->adminLog($model);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Region model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        $post = Yii::$app->request->post();
        if (isset($post['Region']['parent_id'])) {
            $p = 0;
            foreach ($post['Region']['parent_id'] as $id) {
                if ($id == 0) {
                    break;
                }
                $p = $id;
            }
            $post['Region']['parent_id'] = $p;
        }
        
        if ($model->load($post) && $model->save()) {
            $this->adminLog($model);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Region model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $model = $this->findModel($id);
        
        if($model->childCount>0){
            throw new BadRequestHttpException('含有子节点，请先删除！');
        }
        
        $model->delete();
        $this->adminLog($model);

        return $this->redirect(['index']);
    }

    /**
     * Finds the Region model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Region the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Region::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionGetParent($id) {
        $model = $this->findModel($id);

        return $this->renderPartial('_parent', [
                    'model' => $model,
        ]);
    }

}
